package com.workcell.workdata.xthings.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.workcell.workdata.xthings.constant.HardwareStateEnum;
import com.workcell.workdata.xthings.dto.BaseVo;
import com.workcell.workdata.xthings.entity.BaseVoiceSynthesis;
import com.workcell.workdata.xthings.mapper.BaseVoiceSynthesisMapper;
import com.workcell.workdata.xthings.service.BaseVoiceSynthesisService;
import org.springframework.stereotype.Service;

/**
 * @author Admin
 * @description 针对表【t_base_voice_synthesis(语音合成播放表)】的数据库操作Service实现
 * @createDate 2023-08-03 09:17:29
 */
@Service
public class BaseVoiceSynthesisServiceImpl extends ServiceImpl<BaseVoiceSynthesisMapper, BaseVoiceSynthesis>
        implements BaseVoiceSynthesisService {

    @Override
    public Page<BaseVoiceSynthesis> vaguePage(BaseVo baseVo) {
        LambdaQueryWrapper<BaseVoiceSynthesis> bseVoiceSynthesisCondition = new QueryWrapper<BaseVoiceSynthesis>().lambda()
                .and(ObjectUtil.isNotEmpty(baseVo.getKeyWord()), i -> i
                        .like(BaseVoiceSynthesis::getName, baseVo.getKeyWord())
                        .or()
                        .like(BaseVoiceSynthesis::getManufacturer, baseVo.getKeyWord()))
                .eq(ObjectUtil.isNotEmpty(baseVo.getState()), BaseVoiceSynthesis::getState, baseVo.getState())
                .and(ObjectUtil.isNotEmpty(baseVo.getStartTime()) || ObjectUtil.isNotEmpty(baseVo.getEndTime())
                        , i -> i.between(BaseVoiceSynthesis::getCreateTime, baseVo.getStartTime(), baseVo.getEndTime()));
        Page<BaseVoiceSynthesis> page = new Page<>(baseVo.getPageNum(), baseVo.getPageSize());
        page.addOrder(OrderItem.desc("create_time"));
        return this.page(page, bseVoiceSynthesisCondition);
    }

    @Override
    public Boolean updateStateById(BaseVoiceSynthesis baseVoiceSynthesis) {
        Assert.isTrue(ObjectUtil.isNotEmpty(baseVoiceSynthesis.getId()), "ID不能为空");
        Assert.isTrue(ObjectUtil.isNotEmpty(baseVoiceSynthesis.getState()), "状态不能为空");
        // 查询该阀门的状态
        BaseVoiceSynthesis baseOld = this.getById(baseVoiceSynthesis.getId());
        Assert.isTrue(ObjectUtil.isNotEmpty(baseOld), "无该条记录，请核实您的操作");
        if (HardwareStateEnum.INUSE.getCode().equals(baseOld.getState())
                || HardwareStateEnum.SUSPEND.getCode().equals(baseOld.getState())) {
            if (HardwareStateEnum.DRAF.getCode().equals(baseOld.getState())) {
                Assert.isTrue(false, "当状态为使用中或暂停中时，不能修改为草稿状态");
            }
        }
        return this.updateById(baseVoiceSynthesis);
    }
}




